﻿using System;
using System.Runtime.InteropServices;
using System.IO;
using Ooganizer.Platform.Win32;
using Ooganizer.Platform;
using Ooganizer.Server;

namespace Ooganizer.Resolver
{

    /// <summary>
    /// Excel Resolver
    /// </summary>
    public class MSExcelResolver : IResolver, IResolveable
    {
        public const string ProgId = "Excel.Application";

        public bool CanResolve(string processName)
        {
            switch (processName.ToLower())
            {
                case "excel":
                    return true;
                default:
                    return false;
            }
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.Excel.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.Excel.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();
                artifact.Name = Path.GetFileNameWithoutExtension(app.ActiveWorkbook.FullName);
                artifact.Location = app.ActiveWorkbook.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }

    /// <summary>
    /// PowerPoint Resolver
    /// </summary>
    public class MSPowerPointResolver : IResolver, IResolveable
    {
        public const string ProgId = "PowerPoint.Application";

        public bool CanResolve(string processName)
        {
            switch (processName.ToLower())
            {
                case "powerpnt":
                    return true;
                default:
                    return false;
            }
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.PowerPoint.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.PowerPoint.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();
                artifact.Name = Path.GetFileNameWithoutExtension(app.ActivePresentation.FullName);
                artifact.Location = app.ActivePresentation.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }

    /// <summary>
    /// Word Resolver
    /// </summary>
    public class MSWordResolver : IResolver, IResolveable
    {
        public const string ProgId = "Word.Application";

        public bool CanResolve(string processName)
        {
            switch (processName.ToLower())
            {
                case "winword":
                    return true;
                default:
                    return false;
            }
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.Word.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.Word.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();
                artifact.Name = Path.GetFileNameWithoutExtension(app.ActiveDocument.FullName);
                artifact.Location = app.ActiveDocument.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }

    /// <summary>
    /// OneNote Resolver - TO DO
    /// </summary>
    public class MSOneNoteResolver : IResolver, IResolveable
    {
        public const string ProgId = "OneNote.Application";

        public bool CanResolve(string processName)
        {
            switch (processName.ToLower())
            {
                case "onenote":
                    return true;
                default:
                    return false;
            }
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.OneNote.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.OneNote.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();

                //Microsoft.Office.Interop.OneNote.HierarchyScope

                //artifact.Name = Path.GetFileNameWithoutExtension(app.ActiveDocument.FullName);
                //artifact.Location = app.ActiveDocument.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }

    /// <summary>
    /// Visio Resolver
    /// </summary>
    public class MSVisioResolver : IResolver, IResolveable
    {
        public const string ProgId = "Visio.Application";

        public bool CanResolve(string processName)
        {
            switch (processName.ToLower())
            {
                case "visio":
                    return true;
                default:
                    return false;
            }
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.Visio.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.Visio.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();
                artifact.Name = Path.GetFileNameWithoutExtension(app.ActiveDocument.FullName);
                artifact.Location = app.ActiveDocument.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }

    /// <summary>
    /// Access Resolver - TO DO
    /// </summary>
    public class MSAccessResolver : IResolver, IResolveable
    {
        public const string ProgId = "Access.Application";

        public bool CanResolve(string processName)
        {
            switch (processName.ToLower())
            {
                case "msaccess":
                    return true;
                default:
                    return false;
            }
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.Access.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.Access.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();
                //artifact.Name = Path.GetFileNameWithoutExtension(app.AActiveDocument.FullName);
                //artifact.Location = app.ActiveDocument.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }

    /// <summary>
    /// FrontPage Resolver - To DO
    /// </summary>
    public class MSFrontPageResolver : IResolver, IResolveable
    {
        public const string ProgId = "FrontPage.Application";

        public bool CanResolve(string processName)
        {
            // Not sure what executable this maps to ...
            return false;
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.FrontPage.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.FrontPage.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();
                //artifact.Name = Path.GetFileNameWithoutExtension(app.ActiveDocument.FullName);
                //artifact.Location = app.ActiveDocument.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }

    /// <summary>
    /// Publisher Resolver
    /// </summary>
    public class MSPublisherResolver : IResolver, IResolveable
    {
        public const string ProgId = "Publisher.Application";

        public bool CanResolve(string processName)
        {
            switch (processName.ToLower())
            {
                case "mspub":
                    return true;
                default:
                    return false;
            }
        }

        public ArtifactGroup GetArtifacts(IntPtr hWnd)
        {
            Microsoft.Office.Interop.Publisher.Application app = null;
            app = Win32Functions.GetCOMObject(ProgId) as Microsoft.Office.Interop.Publisher.Application;

            ArtifactGroup artifacts = new ArtifactGroup();
            if (app != null)
            {
                Artifact artifact = new Artifact();
                artifact.Name = Path.GetFileNameWithoutExtension(app.ActiveDocument.FullName);
                artifact.Location = app.ActiveDocument.FullName;
                artifacts.AddPrimary(artifact);
            }
            else
                Log.Error(string.Format("{0}() - Error occured casting to {1}",
                                        System.Reflection.MethodBase.GetCurrentMethod().Name, app.GetType().Name));

            return artifacts;
        }

        #region log4net declaration

        private log4net.ILog Log
        {
            get
            {
                if (_logWrapper == null)
                {
                    _logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                }

                return _logWrapper.Log;
            }
        }

        private LogServerClientWrapper _logWrapper;

        #endregion
    }
}
